<?php

namespace App\Http\Controllers\Estate;

use App\Http\Controllers\Controller;
use App\Models\AdminEstate;
use App\Models\Store;
use App\Services\GrabService;
use App\Services\UserService;

class GrabController extends Controller
{

    /**
     * @var GrabService
     */
    public $service;

    /**
     * GrabController constructor.
     */
    public function __construct()
    {
        $this->service = new GrabService();
    }


    /**
     * 2021/3/19--14:03
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:grabOrderList
     * @effect:抢单列表
     */
    public function grabOrderList(AdminEstate $admin_model, Store $store_model)
    {
        try {
            $userService = new UserService();
            $user_info = $userService->getUserInfo('estate');
            $list = $this->service->getList();
            $user_list = $admin_model->with('roles')
                ->orderBy('id', 'desc')
                ->select('username as label', 'id as value');
            if ($user_info['pid'] != 0) {
                $user_list->where('id', $user_info['id']);
            }
            $user_list = $user_list->get();
            $store_list = $store_model->where('store_verify', 3)
                ->orderBy('id', 'desc')
                ->select('store_name as label', 'id as value')
                ->get();
            $data = [
                'list' => $list['data'],
                'user_list' => $user_list,
                'store_list' => $store_list
            ];
            return $this->success($data);
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }


    /**
     * 2021/3/19--11:30
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:grabOrder
     * @effect:抢单
     */
    public function grabOrder()
    {
        try {
            return $this->service->add();
        } catch (\Exception $e) {
            return $this->format_error($e->getMessage());
        }
    }


    /**
     * 2021/3/20--17:42
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:grabOrderStatusChenge
     * @effect:抢单状态修改
     */
    public function grabOrderStatusChenge()
    {
        $ids = explode(',', request()->ids);
        $stop = request()->stop;
        try {
            $this->service->changeStatus($ids, $stop);
            return $this->format([]);
        } catch (\Exception $e) {
            return $this->format_error($e->getMessage());
        }
    }
}
